<?php
/*
---------------------------------------------------------------------------
status/detail_data.php for SALAM Frontend

Copyright 2009, 2010 Jacob McEntire

    This file is part of SALAM.

    SALAM is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SALAM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SALAM.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
*/
require_once("../../library/frontend.php");

$location_result = run_query("SELECT id, name FROM locations ORDER BY name ASC");
if (mysql_num_rows($location_result) != NULL) {
	while(list($location_id, $location_name) = mysql_fetch_array($location_result))	{
		echo '<h3>', $location_name, '</h3>
		<table class="status">';
		$group_result = run_query("SELECT id, name FROM groups ORDER BY name ASC");
		if (mysql_num_rows($group_result) != NULL) {
			while(list($group_id, $group_name) = mysql_fetch_array($group_result))	{
				$host_result = run_query("SELECT id, name FROM hosts WHERE location_id = '$location_id' AND group_id = '$group_id' ORDER BY name ASC");
				if (mysql_num_rows($host_result) != NULL) {
					echo '<tr><th colspan=5>', $group_name, '</th></tr>
					<tr><th style="width:200px;">Host:</th><th>Trigger:</th><th>Info:</th><th>Last Check:</th></tr>';
					while(list($host_id, $host_name) = mysql_fetch_array($host_result))	{
						$detail_result = run_query("SELECT hosts_triggers.id, hosts_triggers.last_check, hosts_triggers.current_state, hosts_triggers.current_info, triggers.name FROM hosts_triggers, triggers WHERE hosts_triggers.host_id = '$host_id' AND hosts_triggers.trigger_id = triggers.id ORDER BY triggers.name ASC");
						if (mysql_num_rows($detail_result) != NULL) {
							$state_result = run_query("SELECT MAX(current_state) as current_state FROM hosts_triggers WHERE host_id = '$host_id' LIMIT 1");
							list($host_state) = mysql_fetch_array($state_result);
							echo '<tr><td colspan="4" class="', return_state($host_state), '"><a href="host.php?id=', $host_id, '">', $host_name, '</a></td></tr>';
							while(list($host_trigger_id, $last_check, $state, $info, $trigger_name) = mysql_fetch_array($detail_result))	{
								echo '<tr><td>&nbsp;</td><td class="';
								if ($state > 0)
									echo return_state($state);
								echo '"><a href="trigger.php?id=', $host_trigger_id, '">', $trigger_name, '</a></td><td>', $info, '</td><td>', date("D M j G:i:s T Y", $last_check), '</td></tr>';
							}
						}
					}
				}
			}
		}
		echo '</table>';
	}
}
echo '<div class="current_time">Current Date/Time: ', date("D M j G:i:s T Y"), '</div>';
?>